package wubj.com.tendencydemo.act;

import android.content.Intent;
import android.graphics.Color;
import android.graphics.Rect;
import android.graphics.drawable.ColorDrawable;
import android.os.Build;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.Gravity;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.PopupWindow;
import android.widget.TextView;

import wubj.com.tendencydemo.R;
import wubj.com.tendencydemo.application.ToastU;
import wubj.com.tendencydemo.calendar.DateRangePicker;
import wubj.com.tendencydemo.view.DateRangeDialog;

public class HomeActivity extends AppCompatActivity {

    private PopupWindow popupWindow;
    private View popView;
    private Button bt4;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_home);
        bt4 = (Button) findViewById(R.id.bt4);
        bt4.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                showPop();
            }
        });
        initPopWindow();
    }

    private void initPopWindow() {
        popView = View.inflate(this, R.layout.layout_date_range, null);
        TextView bt_left = (TextView) popView.findViewById(R.id.bt_left);
        TextView bt_right = (TextView) popView.findViewById(R.id.bt_right);
        final DateRangePicker drp = (DateRangePicker) popView.findViewById(R.id.date_range_picker);
//        ppshow = AnimationUtils.loadAnimation(this, R.anim.ppshow_from_top); //初始化popupWindow动画
        bt_left.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                popDismiss();
            }
        });
        bt_right.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String str = "起始日期:" + drp.getStartDate() + "结束日期:" + drp.getEndDate();
                ToastU.showSingleS(str);
                popDismiss();
            }
        });

    }

    public void jump1(View view) {
        startActivity(new Intent(this, MainActivity.class));
    }

    public void jump2(View view) {
        startActivity(new Intent(this, Main2Activity.class));
    }

    public void jump3(View view) {
        startActivity(new Intent(this, DateRangeActivity.class));
    }

    protected void showPop() {
        popDismiss();
//        ViewPropertyAnimator.animate(mImageview_arrow_bottom).rotationBy(180).setDuration(300).start();
        popupWindow = new PopupWindow(popView, ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams
                .MATCH_PARENT, true);
        popupWindow.setOutsideTouchable(false);
        popupWindow.setFocusable(false);
        // 必须设置背景颜色,动画效果才会生效
        popupWindow.setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
        if (Build.VERSION.SDK_INT != 24) {
            popupWindow.showAsDropDown(bt4);
        } else {
            // 适配 android 7.0
            Rect frame = new Rect();
            getWindow().getDecorView().getWindowVisibleDisplayFrame(frame);
            int mStatusBarHeight = frame.top;
            popupWindow.showAtLocation(bt4, Gravity.NO_GRAVITY, 0, bt4.getHeight() + mStatusBarHeight);
        }
        // 可以添加任意的动画资源
//        popView.startAnimation(ppshow);
    }

    /**
     * popupWindow的关闭方法
     */
    protected void popDismiss() {
        if (popupWindow != null) {
            popupWindow.dismiss();
            popupWindow = null;
        }
    }

    public void jump5(View view) {
        final DateRangeDialog dialog = new DateRangeDialog.Builder(this).build();
        dialog.setListener();
        dialog.setRightListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                ToastU.showSingleS("");
            }
        });
        dialog.show();
    }

    @Override
    public boolean dispatchTouchEvent(MotionEvent event) {
        if (popupWindow != null && popupWindow.isShowing()) {
            return false;
        }
        return super.dispatchTouchEvent(event);
    }
}
